package com.tencent.wxcloudrun.service;

import com.tencent.wxcloudrun.dao.CounterMapper;
import com.tencent.wxcloudrun.dto.CounterRequest;
import com.tencent.wxcloudrun.model.Counter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author zhe.wang@weimob.com
 * @date 2023/4/3 下午4:43
 */
@Service
public class CounterService {

    @Autowired
    CounterMapper counterMapper;


    public void add(CounterRequest request) {

        Counter counter = get(request.getId());
        if (counter == null) {
            counter = new Counter();
            counter.setId(request.getId());
            counter.setCount(request.getCount());
            counter.setCreatedAt(LocalDateTime.now());
            counter.setUpdatedAt(LocalDateTime.now());
            counterMapper.insertTemplate(counter);
        } else {
            counter.setCount(request.getCount());
            counter.setUpdatedAt(LocalDateTime.now());
            counterMapper.updateTemplateById(counter);
        }
    }


    public Counter get(Integer id) {
        return counterMapper.createLambdaQuery().andEq(Counter::getId, id).single();
    }
}
